﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CodePlex.Resourceful.Microsoft.Azure
{
    public class AzureQueueConnection : BaseAzureConnection<AzureQueueClient>
    {
        public AzureQueueConnection(AzureQueueClient client)
            : base(client)
        {

        }

        public bool IsQueuePresent(string queueName)
        {
            ClientResponse response = this.Client.HeadQueue(queueName);
            return response.HttpStatus.Equals(HttpStatus.OK);
        }

        public void CreateQueue(string queueName)
        {
            this.CreateQueue(queueName, null);
        }
        public void CreateQueue(string queueName, NameValuePairs metadata)
        {
//The queue name must be a valid DNS name, conforming to the following naming rules:

//   1. A queue name must start with a letter or number, and may contain only letters, numbers and the dash (-) character.
//   2. The first and last letters in the queue name must be alphanumeric. The dash (-) character may not be the first or last letter.
//   3. All letters in a queue name must be lowercase.
//   4. A queue name must be from 3 through 63 characters long. 


            ClientResponse response = this.Client.PutQueue(queueName, metadata);
            Assert(response, HttpStatus.Created);
        }


        public QueueListing ListQueues()
        {
            return this.ListQueues( null, null, null);
        }
        public QueueListing ListQueues(string prefix, string marker, int? maxResults)
        {
            ClientResponse response = this.Client.GetQueues( prefix, marker, maxResults);
            Assert(response, HttpStatus.OK);
            return QueueListing.Parse(response.AsXmlReader());
        }



        public void DeleteQueue(string queueName)
        {
            ClientResponse response = this.Client.DeleteQueue(queueName);
            Assert(response, HttpStatus.NoContent);
        }


        public NameValuePairs GetQueueMetadata(string queueName)
        {
            ClientResponse response = this.Client.HeadQueue(queueName);
            Assert(response, HttpStatus.OK);
            return GetMetadata(response, false);
        }

        public void SetQueueMetadata(string queueName, NameValuePairs metadata)
        {
            ClientResponse response = this.Client.PutQueueMetadata(queueName, metadata);
            Assert(response, HttpStatus.NoContent);
        }




        public void ClearMessages(string queueName)
        {
            ClientResponse response = this.Client.DeleteMessages(queueName);
            Assert(response, HttpStatus.NoContent);
        }




        public void PutMessage(string queueName, string messageText, TimeSpan? messageTTL)
        {
            int? ttl = !messageTTL.HasValue?null:(int?)messageTTL.Value.TotalSeconds;
           
            ClientResponse response = this.Client.PostMessage(queueName,messageText,ttl);
            Assert(response, HttpStatus.Created);

        }


        public MessageListing PeekMessages(string queueName, int? numMessages)
        {
            ClientResponse response = this.Client.GetMessagesPeekOnly(queueName, numMessages);
            Assert(response, HttpStatus.OK);
            return MessageListing.Parse(response.AsXmlReader());
        }

        public MessageListing GetMessages(string queueName, int? numMessages, TimeSpan? visibilityTimeout)
        {
            int? vis = !visibilityTimeout.HasValue?null:(int?)visibilityTimeout.Value.TotalSeconds;

            ClientResponse response = this.Client.GetMessages(queueName, numMessages,vis);
            Assert(response, HttpStatus.OK);
            return MessageListing.Parse(response.AsXmlReader());
        }



        public void DeleteMessage(string queueName, string messageID, string popReceipt)
        {
            ClientResponse response = this.Client.DeleteMessage(queueName, messageID, popReceipt);
            Assert(response, HttpStatus.NoContent);
        }


    }
}
